package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.Utils.BaseContext;
import com.itheima.commo.R;
import com.itheima.entity.OrderDetail;
import com.itheima.entity.Orders;
import com.itheima.entity.ShoppingCart;
import com.itheima.mapper.OrdersDetailMapper;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.service.OrderDetailService;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单详情业务层接口类
 */
@Service
@Transactional
@Slf4j
public class OrderDetailServiceImpl implements OrderDetailService {
    @Autowired
    private OrdersDetailMapper ordersDetailMapper;

    //注入购物车业务接口
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    /**
     * 循环遍历 插入订单详情表
     * 传入订单id
     *
     * 1根据当前用户id查询当前用户购物列表数据
     * 2将购物车列表数据copy到订单详情表中进行保存
     * 3循环插入orderDetail表中
     * @param ordersId
     */
    @Override
    public void insert(Long ordersId) {
        //1.根据当前用户id查询购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartService.list();
        //2.将购物车数据copy到订单详情表中
        if (!CollectionUtils.isEmpty(shoppingCartList)) {
            //对购物车数据进行遍历
            shoppingCartList.stream().map(
                    shoppingCart -> {
                        //shoppingCart 购物车每一条记录
                        OrderDetail orderDetail = new OrderDetail();
                        //从购物车对象中copy数据到订单详情中
                        BeanUtils.copyProperties(shoppingCart, orderDetail);
                        //设置orderDetail表中订单id
                        orderDetail.setOrderId(ordersId);
                        //将orderDetail插入表中
                        ordersDetailMapper.insert(orderDetail);
                        log.debug("**循环插入订单详情表成功了**");
                        return orderDetail;
                    }
                    //3.循环插入orderDetail表中
            ).collect(Collectors.toList());
        }
        //3 清除当前用户购物车数据
        shoppingCartService.clean();
        log.debug("**清空购物车成功了**");
    }

    /**
     * 客户端再来一单
     *
     * @param id
     * @return
     */
    @Override
    public R<Orders> again(Orders id) {
        LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderDetail::getOrderId,id);
        List<OrderDetail> orderDetails = ordersDetailMapper.selectList(lqw);
        for (OrderDetail orderDetail : orderDetails) {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(orderDetail, shoppingCart);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCartMapper.insert(shoppingCart);
        }
        return null;
    }
}
